package com.szuka.java.ai.langchain4j.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szuka.java.ai.langchain4j.entity.DoctorSchedule;
import com.szuka.java.ai.langchain4j.mapper.DoctorScheduleMapper;
import com.szuka.java.ai.langchain4j.service.DoctorScheduleService;
import org.springframework.stereotype.Service;

@Service
public class DoctorScheduleServiceImpl extends ServiceImpl<DoctorScheduleMapper, DoctorSchedule> implements DoctorScheduleService {
    
    @Override
    public DoctorSchedule getSchedule(String department, String date, String time, String doctorName) {
        LambdaQueryWrapper<DoctorSchedule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DoctorSchedule::getDepartment, department);
        queryWrapper.eq(DoctorSchedule::getDate, date);
        queryWrapper.eq(DoctorSchedule::getTime, time);
        
        if (doctorName != null && !doctorName.isEmpty()) {
            queryWrapper.eq(DoctorSchedule::getDoctorName, doctorName);
        }
        
        return baseMapper.selectOne(queryWrapper);
    }
    
    @Override
    public boolean increaseCurrentAppointments(Long scheduleId) {
        DoctorSchedule schedule = baseMapper.selectById(scheduleId);
        if (schedule != null && schedule.getCurrentAppointments() < schedule.getMaxAppointments()) {
            schedule.setCurrentAppointments(schedule.getCurrentAppointments() + 1);
            return baseMapper.updateById(schedule) > 0;
        }
        return false;
    }
    
    @Override
    public boolean decreaseCurrentAppointments(Long scheduleId) {
        DoctorSchedule schedule = baseMapper.selectById(scheduleId);
        if (schedule != null && schedule.getCurrentAppointments() > 0) {
            schedule.setCurrentAppointments(schedule.getCurrentAppointments() - 1);
            return baseMapper.updateById(schedule) > 0;
        }
        return false;
    }
} 